﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using PrintLabel;

namespace Packaging
{
	// Token: 0x02000003 RID: 3
	public class PrintLabelByBarTender
	{
		// Token: 0x06000018 RID: 24 RVA: 0x0000845C File Offset: 0x0000665C
		public static bool Print_Label(ListView listbPrint, string sBTWLabelName, string sProgramName)
		{
			Setup setup = new Setup();
			setup.Open("BARTENDER");
			bool result;
			try
			{
				int num = 1;
				string empty = string.Empty;
				string text = string.Concat(new string[]
				{
					Application.StartupPath,
					"\\",
					sProgramName,
					"\\",
					sBTWLabelName,
					".btw"
				});
				string text2 = string.Concat(new string[]
				{
					Application.StartupPath,
					"\\",
					sProgramName,
					"\\",
					sBTWLabelName,
					".lst"
				});
				string text3 = string.Concat(new string[]
				{
					Application.StartupPath,
					"\\",
					sProgramName,
					"\\",
					sBTWLabelName,
					".dat"
				});
				string text4 = Application.StartupPath + "\\" + sProgramName + "\\PrintGo.bat";
				string text5 = Application.StartupPath + "\\" + sProgramName + "\\PrintLabel.bat";
				bool flag = !File.Exists(text);
				if (flag)
				{
					SajetCommon.Show_Message(SajetCommon.SetLanguage("Label File Not Found") + " :" + Environment.NewLine + text, 0);
					result = false;
				}
				else
				{
					bool flag2 = !File.Exists(text3);
					if (flag2)
					{
						SajetCommon.Show_Message(SajetCommon.SetLanguage("Label File Not Found") + " :" + Environment.NewLine + text3, 0);
						result = false;
					}
					else
					{
						bool flag3 = !File.Exists(text5);
						if (flag3)
						{
							SajetCommon.Show_Message(SajetCommon.SetLanguage("Label File Not Found") + " :" + Environment.NewLine + text5, 0);
							result = false;
						}
						else
						{
							bool flag4 = File.Exists(text2);
							if (flag4)
							{
								File.Delete(text2);
							}
							StreamReader streamReader = new StreamReader(text3);
							string text6 = streamReader.ReadLine().Trim();
							streamReader.Close();
							bool flag5 = string.IsNullOrEmpty(text6);
							if (flag5)
							{
								SajetCommon.Show_Message(sBTWLabelName + ".dat" + Environment.NewLine + SajetCommon.SetLanguage("Field file can not be empty"), 0);
								result = false;
							}
							else
							{
								PrintLabelByBarTender.WriteToTxt(text2, text6);
								for (int i = 0; i < listbPrint.Items.Count; i++)
								{
									PrintLabelByBarTender.WriteToTxt(text2, listbPrint.Items[i].Text);
								}
								streamReader = new StreamReader(text5);
								string text7 = streamReader.ReadLine().Trim();
								streamReader.Close();
								bool flag6 = text7.IndexOf("@PATH1") >= 0;
								if (flag6)
								{
									text7 = text7.Replace("@PATH1", "\"" + text + "\"");
								}
								bool flag7 = text7.IndexOf("@PATH2") >= 0;
								if (flag7)
								{
									text7 = text7.Replace("@PATH2", "\"" + text2 + "\"");
								}
								bool flag8 = text7.IndexOf("@QTY") >= 0;
								if (flag8)
								{
									text7 = text7.Replace("@QTY", num.ToString());
								}
								bool flag9 = File.Exists(text4);
								if (flag9)
								{
									File.Delete(text4);
								}
								File.AppendAllText(text4, text7, Encoding.Default);
								PrintLabelByBarTender.WinExec(text4, 0);
								Thread.Sleep(500);
								setup.Close("BARTENDER");
								result = true;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				setup.Close("BARTENDER");
				result = false;
			}
			finally
			{
				Thread.Sleep(500);
				setup.Close("BARTENDER");
			}
			return result;
		}

		// Token: 0x06000019 RID: 25 RVA: 0x00008820 File Offset: 0x00006A20
		public static bool Print_Label(ListView listbPrint, string sBTWLabelName, string sProgramName, ProgressBar pBar)
		{
			Setup setup = new Setup();
			setup.Open("BARTENDER");
			bool result;
			try
			{
				pBar.Minimum = 0;
				pBar.Maximum = listbPrint.Items.Count - 1;
				pBar.Visible = true;
				int num = 1;
				string empty = string.Empty;
				string text = string.Concat(new string[]
				{
					Application.StartupPath,
					"\\",
					sProgramName,
					"\\",
					sBTWLabelName,
					".btw"
				});
				string text2 = string.Concat(new string[]
				{
					Application.StartupPath,
					"\\",
					sProgramName,
					"\\",
					sBTWLabelName,
					".lst"
				});
				string text3 = string.Concat(new string[]
				{
					Application.StartupPath,
					"\\",
					sProgramName,
					"\\",
					sBTWLabelName,
					".dat"
				});
				string text4 = Application.StartupPath + "\\" + sProgramName + "\\PrintGo.bat";
				string text5 = Application.StartupPath + "\\" + sProgramName + "\\PrintLabel.bat";
				bool flag = !File.Exists(text);
				if (flag)
				{
					SajetCommon.Show_Message(SajetCommon.SetLanguage("Label File Not Found") + " :" + Environment.NewLine + text, 0);
					result = false;
				}
				else
				{
					bool flag2 = !File.Exists(text3);
					if (flag2)
					{
						SajetCommon.Show_Message(SajetCommon.SetLanguage("Label File Not Found") + " :" + Environment.NewLine + text3, 0);
						result = false;
					}
					else
					{
						bool flag3 = !File.Exists(text5);
						if (flag3)
						{
							SajetCommon.Show_Message(SajetCommon.SetLanguage("Label File Not Found") + " :" + Environment.NewLine + text5, 0);
							result = false;
						}
						else
						{
							bool flag4 = File.Exists(text2);
							if (flag4)
							{
								File.Delete(text2);
							}
							StreamReader streamReader = new StreamReader(text3);
							string text6 = streamReader.ReadLine().Trim();
							streamReader.Close();
							bool flag5 = string.IsNullOrEmpty(text6);
							if (flag5)
							{
								SajetCommon.Show_Message(sBTWLabelName + ".dat" + Environment.NewLine + SajetCommon.SetLanguage("Field file can not be empty"), 0);
								result = false;
							}
							else
							{
								PrintLabelByBarTender.WriteToTxt(text2, text6);
								for (int i = 0; i < listbPrint.Items.Count; i++)
								{
									PrintLabelByBarTender.WriteToTxt(text2, listbPrint.Items[i].Text);
								}
								streamReader = new StreamReader(text5);
								string text7 = streamReader.ReadLine().Trim();
								streamReader.Close();
								bool flag6 = text7.IndexOf("@PATH1") >= 0;
								if (flag6)
								{
									text7 = text7.Replace("@PATH1", "\"" + text + "\"");
								}
								bool flag7 = text7.IndexOf("@PATH2") >= 0;
								if (flag7)
								{
									text7 = text7.Replace("@PATH2", "\"" + text2 + "\"");
								}
								bool flag8 = text7.IndexOf("@QTY") >= 0;
								if (flag8)
								{
									text7 = text7.Replace("@QTY", num.ToString());
								}
								bool flag9 = File.Exists(text4);
								if (flag9)
								{
									File.Delete(text4);
								}
								File.AppendAllText(text4, text7, Encoding.Default);
								PrintLabelByBarTender.WinExec(text4, 0);
								result = true;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				result = false;
			}
			finally
			{
				pBar.Visible = false;
				setup.Close("BARTENDER");
			}
			return result;
		}

		// Token: 0x0600001A RID: 26
		[DllImport("kernel32.dll")]
		public static extern int WinExec(string exeName, int operType);

		// Token: 0x0600001B RID: 27 RVA: 0x00008BE0 File Offset: 0x00006DE0
		private static void WriteToTxt(string sFile, string sData)
		{
			StreamWriter streamWriter = null;
			try
			{
				bool flag = File.Exists(sFile);
				if (flag)
				{
					streamWriter = new StreamWriter(sFile, true, Encoding.Default);
				}
				else
				{
					streamWriter = File.CreateText(sFile);
				}
				streamWriter.WriteLine(sData);
			}
			finally
			{
				streamWriter.Close();
			}
		}
	}
}
